package com.gr

import grails.test.mixin.*
import org.junit.*

import com.gr.Project;

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Project)
class ProjectTests {

	def project
	def company = new Company(title: 'duinc', email: 'duinc@gmail.com', startDate: new Date("2001/1/18"))
	def division = new Division(title: 'DivisionA', email: 'divisiona@gmail.com', startDate: new Date("2011/3/21"), companyTo: company)
	
	@Before
	void setUp() {
		// Make sure we can invoke validate() on our User domain object.
		mockForConstraintsTests(Project)
		// Set up default project, so we can easily test single properties.
		project = new Project(title:"ProjectA1", email:"projectA1@duorg.com", startDate:new Date("8/18/2012"), divisionTo:division)
	}

	/**
	 * 	title(unique: true)
	 */
	void testConstraintsTitleUnique() {

		def test = new Project(title: 'ProjectA1', email: 'somediv@gmail.com', startDate: new Date("2005/7/2"), divisionTo: division)
		mockForConstraintsTests(Project, [test])

		assertFalse project.validate()
		assertEquals 'unique', project.errors['title']

		project.title = 'some'
		assertTrue project.validate()
	}

	/**
	 * title(blank: false)
	 */	
	void testConstraintsTitleBlank() {
		project.title = ''
		assertFalse project.validate()
		assertEquals 'blank', project.errors['title']

		project.title = 'notempty'
		assertTrue project.validate()
	}

	/**
	 * title(size: 2..50)
	 */
	void testConstraintsTitleSize() {
		project.title = 'd'
		assertFalse project.validate()
		assertEquals 'size', project.errors['title']

		project.title = '123456789012345678901234567890123456789012345678901'
		assertFalse project.validate()
		assertEquals 'size', project.errors['title']

		project.title = 'normalsize'
		assertTrue project.validate()
	}

	/**
	 * email(email: true)
	 */
	void testConstraintsEmailEmail() {
		project.email = 'somesting'
		assertFalse project.validate()
		assertEquals 'email', project.errors['email']

		project.email = 'valid@email.ru'
		assertTrue project.validate()
	}

	/**
	 * email(nullable: true)
	 */
	void testConstraintsEmailNullable() {
		project.email = ''
		assertTrue project.validate()
		assertNull project.errors['email']
	}

	/**
	 * startDate(nullable: true)
	 */
	void testConstraintsStartDateNullable() {
		project.startDate = null
		assertTrue project.validate()
		assertNull project.errors['startDate']
	}
	
	/**
	 * divisionTo(blank: false)
	 */
	void testConstraintsCompanyToBlank() {
		project.divisionTo = null
		assertFalse project.validate()
		assertEquals 'nullable', project.errors['divisionTo']
		
		project.divisionTo = division
		assertTrue project.validate()
	}
}
